package cn.edu.zut.soft.student.service.course.impl;

import cn.edu.zut.soft.student.bean.PageBean;
import cn.edu.zut.soft.student.dao.ClassMapper;
import cn.edu.zut.soft.student.dao.CourseMapper;
import cn.edu.zut.soft.student.model.SelectCourseStudents;
import cn.edu.zut.soft.student.model.Student;
import cn.edu.zut.soft.student.service.course.SelectCourseStudentsBo;
import cn.edu.zut.soft.student.service.course.vo.SelectCourseStudentsQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author ChrisPhantom
 * @CreatDate 2021/02/21 21:57
 */

@Service
public class SelectCourseStudentsBoImpl implements SelectCourseStudentsBo {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private ClassMapper classMapper;


    @Override
    public PageBean<List<SelectCourseStudents>> querySelectCourseStudents(SelectCourseStudentsQuery scsq) {
        List<SelectCourseStudents> selectCourseStudents = courseMapper.selectCourseStudents().stream().distinct().collect(Collectors.toList());
        if (scsq.getCoursetype() != null && scsq.getCoursetype() != "") {
            selectCourseStudents = selectCourseStudents.stream().filter(scs -> scs.getCourse().getCoursetype().equals(scsq.getCoursetype())).collect(Collectors.toList());
        }
        if (scsq.getCoursename() != null && scsq.getCoursename() != "") {
            selectCourseStudents = selectCourseStudents.stream().filter(scs -> scs.getCourse().getCoursename().contains(scsq.getCoursename())).collect(Collectors.toList());
        }
        long total = selectCourseStudents.size();
        selectCourseStudents = selectCourseStudents.stream().skip(scsq.getOffset()).limit(scsq.getLimit()).collect(Collectors.toList());
        return new PageBean<>(selectCourseStudents, total);
    }

    @Override
    public List<Student> queryCourseStudents(String courseid) {
        List<Student> students = courseMapper.queryCourseStudentsByID(courseid);
        students.forEach(student -> student.setStudentclassid(classMapper.selectByPrimaryKey(student.getStudentclassid()).getClassname()));
        return students;
    }
}
